package com.wifi.unti;

//获取 数据 连接 的信息

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

public class DBHelper {

    private static Properties properties = null;
    private static DruidDataSource druidDataSource = new DruidDataSource();
    private static QueryRunner queryRunner = new QueryRunner(getDataSource());

    static {
        properties = new Properties();
        try {
            properties.load(DBHelper.class.getClassLoader().getResourceAsStream("./jdbc.properties"));
            try {
                druidDataSource.setDriverClassName(getValue("driver"));
                druidDataSource.setUrl(getValue("dbUrl"));
                druidDataSource.setUsername(getValue("username"));
                druidDataSource.setPassword(getValue("password"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static DruidDataSource getDataSource() {
        return druidDataSource;
    }

    private static String getValue(String key) throws Exception {
        Objects.requireNonNull(key);
        if (properties.containsKey(key)) {
            return properties.getProperty(key);
        }
        throw new Exception("没有此key对于的value值" + key);
    }


    //传参为sql语句及对象
    public static Long insert(String sql, Object... args) throws SQLException {
        long i = 0;
        DruidDataSource druidDataSource = getDataSource();
        i = queryRunner.insert(sql, new ScalarHandler<Long>(), args);
        return i;
    }

    //传参为sql语句及对象
    public static int update(String sql, Object... args) throws SQLException {
        int i = 0;
        DruidDataSource druidDataSource = getDataSource();
        i = queryRunner.update(sql, args);
        return i;
    }

    //传参为sql语句，第二个obj为entity实体类，可以直接 {new 实体类名()} 传入 ，及对象args 获取后object 直接java调用需要强转，json.toJSONString无需强转
    public static Object select(String sql, Object obj, Object... args) throws SQLException {
        Object objNow = null;
        ResultSetHandler<Object> h = new BeanHandler<>(obj.getClass(), new BasicRowProcessor(new GenerousBeanProcessor()));
        objNow = queryRunner.query(sql, h, args);
        return objNow;
    }

    //传参为sql语句，第二个obj为entity实体类，可以直接 {new 实体类名()} 传入 ，及对象args 获取后object 直接java调用需要强转，json.toJSONString无需强转
    public static List<Object> selectAll(String sql, Object obj, Object... args) throws SQLException {
        List<Object> objNow = null;
        BeanListHandler<Object> h = new BeanListHandler<>(obj.getClass(), new BasicRowProcessor(new GenerousBeanProcessor()));
        objNow = queryRunner.query(sql, h, args);
        return objNow;
    }

    //废弃方法
    public static Connection getConn() {
        Connection connection = null;
        try {
            Class.forName(getValue("driver"));
            connection = DriverManager.getConnection(getValue("dbUrl"), getValue("username"), getValue("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("废弃方法 请调用getDataSource()");
        return connection;
    }

    //废弃方法 请调用线程池
    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
                System.out.println("方法已废弃但可用，请调用线程池");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

